﻿using System;
using System.Drawing;
using FrameWork.Classes;
using FrameWork.enums;
using FrameWork.InterFace;
using FrameWork.Properties;
using Timer = System.Windows.Forms.Timer;

namespace FrameWork.Controls
{
   public  class DirectUIProgressBar:DirectUIPanel
    {
       private DirectUILabel _lbText;
       private DirectUIButton _btClose;
       private Timer _timer;
       public event EventHandler Cancel;
       public DirectUIProgressBar():base()
       {
           this.Height = 20;
           this.Width = 240;
           this.BackGroundImage =Resource.ProgressBar;
           Init();
       }
       public void OnCancel(EventArgs e)
       {
           if (Cancel != null)
           {
               Cancel(this, e);
           }
       }
       private void Init()
       {
           _lbText = new DirectUILabel();
           _lbText.Text = "加载中.";
           _lbText.Bounds = new System.Drawing.Rectangle(0, 0, this.Width - 30, this.Height);
           _btClose = new DirectUIButton();
           _btClose.Text = "取消";
           _btClose.BackColor = Color.Red;
           _btClose.Shape = ButtonShape.Rectangle;
           _btClose.Bounds = new Rectangle(this.Width - 30, 0, 30, this.Height);
           _btClose.Click += new EventHandler(_btClose_Click);
           _timer = new Timer();
           _timer.Interval = 500;
           _timer.Enabled = false;
           _timer.Tick += new EventHandler(_timer_Tick);
           this.Controls.Add(_lbText);
           this.Controls.Add(_btClose);
       }

       void _btClose_Click(object sender, EventArgs e)
       {
           Hide();
           OnCancel(e);
       }
       public override void Show()
       {
         
           this.Visible = true;
           _timer.Enabled = true;
           if (this.Parent != null)
           {
               DirectUIControlCollection fc = (DirectUIControlCollection)((IMultiControlContainer)this.Parent).Controls;
               fc.Remove(this);
               fc.Add(this);
           }
       }
       public override void Hide()
       {
           this.Visible = false;
           _timer.Enabled = false;
       }
       public string Message
       {
           get;
           set;
       }
       void _timer_Tick(object sender, EventArgs e)
       {
           if (string.IsNullOrEmpty(_lbText.Text))
           {
               _lbText.Text = ".";
           
               return;
           }
      
           if (_lbText.Text.Length >= 5)
           {
               if (_lbText.Text[_lbText.Text.Length - 5] == '.')
               {
                   _lbText.Text = Message + ".";
                   return;
               }
           }
           if (_lbText.Text.Length >= 4)
           {
               if (_lbText.Text[_lbText.Text.Length - 4] == '.')
               {
                   _lbText.Text = Message + ".....";
                   return;
               }
           }
           if (_lbText.Text.Length >= 3)
           {
               if (_lbText.Text[_lbText.Text.Length - 3] == '.')
               {
                   _lbText.Text = Message + "....";
                   return;
               }
           }
           if (_lbText.Text.Length >= 2)
           {
               if (_lbText.Text[_lbText.Text.Length - 2] == '.')
               {
                   _lbText.Text = Message + "...";
                   return;
               }
           }
           if (_lbText.Text[_lbText.Text.Length - 1] == '.')
           {
               _lbText.Text = Message + "..";
               return;
           }
       }
    }
}
